import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		contentShow: "contentShow",
		shareShow: "shareHide",
		code: "",
		folderCode: 0,
		playTaskId: "",
		name: "",
		currentTimePoints: 0,
		lastTimePoints: 0,
		taskId: 0,
		content: {

		},
		recommendList: [

		]
	},
	share(event) {     //点击分享
		this.setData({
			contentShow: "contentHide",
			shareShow: "shareShow"
		})
	},
	cancel(event) {      //取消分享
		this.setData({
			contentShow: "contentShow",
			shareShow: "shareHide",
		})
	},

	goShortVideoDetail(event) {
		wx.navigateTo({
			url: '../shortVideoDetail/shortVideoDetail?assetType=tv'
		})
	},

	onLoad(params) {
		wx.getSystemInfo({
			success: (res) => {   //播放器宽度设置
				this.setData({
					screenWidth: res.windowWidth,
					screenHeight: res.screenHeight
				});
				var playTaskId = this.formatDate("yyyyMMwx");
				var random = Math.round(Math.random() * 99999999999) + 100000000000;
				playTaskId += random;
				//从哪个页面跳转的标记
				var fromPage = params.fromPage;
				this.setData({
					"fromPage": fromPage,
					"timeNow": new Date().getTime(),
					"name": params.name,
				});
				if ("News" == fromPage) {
					//非壳类的资产播放视频
					this.setData({
						"code": params.code,
						"folderCode": params.folderCode,
						"playTaskId": playTaskId,
						"name": params.name,
					});
					//获取内容详情3.1.4
					this.loawxetail();
				} else if ("Topic" == fromPage) {
					//从专题详情页点击专题子内容播放
					this.setData({
						"code": params.subCode,
						"folderCode": params.folderCode,
						"topicId": params.code,
						"playTaskId": playTaskId,
						"name": params.name,
						"topicName": params.topicName,
					});
					//获取内容详情3.1.4
					this.loawxetail();

				} else if ("task" == fromPage) {
					//从学习任务列表跳转过来的
					this.setData({
						"code": params.courseId,
						"taskId": params.taskId,
						"playTaskId": playTaskId,
					});
					//加载任务详情
					this.loadTaskInfo();
				}
			}
		})
	},
	loawxetail: function(e) {
		//调用内容详情接口,获取内容详情(专题子内容的资产,需要传递subCode值) 
		var params = {};
		var newUrl = getApp().globalData.dangjianUrl + "/content/contentQuery";
		if ("Topic" == this.data.fromPage) {
			params["code"] = this.data.topicId + "";
			params["subCode"] = this.data.code + "";
		} else {
			params["code"] = this.data.code + "";
			params["subCode"] = "";
		}
		params["contentName"] = this.data.name + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "内容详情加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var content = respData.data.content;
					var thumbnail = content.thumbnail;
					if (null == thumbnail || "" == thumbnail) {
						var imageFiles = content.imageFiles;
						for (var j in imageFiles) {
							thumbnail = imageFiles[j].url;
							break;
						}
					}
					this.setData({
						"content.refCode": content.refCode,
						"content.publishTime": content.publishTime,
						"content.name": content.name,
						"content.url": thumbnail,
						"content.duration": content.duration,
						"content.type": content.type,
						"content.description": content.description,
						"content.sourceFrom": null == content.sourceFrom || "" == content.sourceFrom ? "华数" : content.sourceFrom
					});
					//调用播放记录查询接口获取视频播放地址
					this.loadPlayUrl();
					//是否收藏
					this.loadFavor();
					//热门推荐列表
					this.recommendList();
				} else {
          this.showPageMsg("alert", respData.data.result.description)
 				}
			}
		);
	},

	//查询学习任务详情
	loadTaskInfo: function(e) {
		var newUrl = getApp().globalData.dangjianUrl + "/study/studyInfo";
		var params = {};
		params["taskId"] = this.data.taskId + '';
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "任务详情接口调用失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var courseItems = respData.data.study.courseItems;
					if (null != courseItems && undefined != courseItems) {
						courseItems = courseItems.courseItem;
						//加载课件基本信息
						this.loawxetail();
					}
        } else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);

	},

	loadPlayUrl: function() {
		//获取图文的正文内容
		var newUrl = getApp().globalData.dangjianUrl + "/content/contentUrlQuery";
		var params = {};
		params["code"] = this.data.code + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params["contentType"] = "1";
		params["from"] = "3";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "视频地址查询失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var playUrls = respData.data.content.playUrls;
					this.setData({
						"content.playUrl": playUrls[0].url,
						"content.itemCode": playUrls[0].subCode
					});
        } else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},

	loadFavor: function() {
		//获取是否已经收藏(如果是专题子内容的收藏,只查询壳的收藏状态)
		var params = {};
		if ("Topic" == this.data.fromPage) {
			params["code"] = this.data.topicId + "";
		} else {
			params["code"] = this.data.code + "";
		}

		params["name"] = this.data.name + "";
		params = JSON.stringify(params);

		var newUrl = getApp().globalData.dangjianUrl + "/member/userContentIsFavor";

		this.httpGet(newUrl, params, "是否收藏接口调用失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var isFavor = respData.data.favor.isFavor;
					//默认为收藏的状态
					var favorIcon = "../../img/collectFillICON.png";
					if (isFavor == "false") {
						favorIcon = "../../img/collectICON.png";
					}
					this.setData({
						"content.favorIcon": favorIcon,
						"content.isFavor": isFavor
					});
        } else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},

	favorContent: function(e) {
		//收藏内容
		var status = e.currentTarget.dataset.status;
		var contentName = e.currentTarget.dataset.name;
		status = "false" == status ? 1 : 0;
		var params = {};
		if ("Topic" == this.data.fromPage) {
			params["code"] = this.data.topicId + "";
		} else {
			params["code"] = this.data.code + "";

		}
		params["siteCode"] = getApp().globalData.siteCode + "";
		params["status"] = status + "";
		params["contentName"] = contentName + "";
		params["contentType"] = "1";
		params["columnCode"] = "";
		params = JSON.stringify(params);
		var newUrl = getApp().globalData.dangjianUrl + "/member/userContentFavor";
		this.httpGet(newUrl, params, "操作失败").then(
			(respData) => {
				var result = respData.data.result.code;
				var isFavor = "true";
				var favorIcon = "../../img/collectFillICON.png";
				if (1 == result) {
					if (0 == status) {
						favorIcon = "../../img/collectICON.png";
						isFavor = "false";
						this.showPageMsg("tips", "该内容收藏取消成功！");
					} else {
						this.showPageMsg("tips", "该内容收藏成功！");
					}
					this.setData({
						"content.favorIcon": favorIcon,
						"content.isFavor": isFavor
					});
        } else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},


	//热门推荐
	recommendList: function(e) {
		var folderCode = this.data.folderCode;
		var refCode = this.data.content.refCode;
		if (null != refCode && undefined != refCode && "" != refCode) {
			folderCode = refCode;
		}
		var newUrl = getApp().globalData.dangjianUrl + "/content/contentRecommend";
		var params = {};
		params["code"] = this.data.code + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "热门推荐加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var contents = respData.data.contents.content;
					var contentList = [];
					for (var index in contents) {
						var content = contents[index];
						//将自己本身去掉
						var code = "";
						if ("topic" == this.data.fromPage) {
							code = this.data.topicId;
						} else {
							code = this.data.code;
						}
						if (code != content.code) {
							var json = {};
							var thumbnail = content.thumbnail;
							if (null == thumbnail || "" == thumbnail) {
								var imageFiles = content.imageFiles;
								for (var j in imageFiles) {
									if (imageFiles[j].type == "240d" || imageFiles[j].type == "QT" || imageFiles[j].type == "180d") {
										thumbnail = imageFiles[j].url;
										break;
									}
								}
							}
							json["name"] = content.name;
							json["publishTime"] = content.publishTime;
							json["url"] = thumbnail;
							json["code"] = content.code;
							json["type"] = content.type;
							json["foldCodes"] = folderCode;
							json["count"] = content.count;
							json["fromNa"] = '' == content.fromNa ? "华数" : content.fromNa;
							json["folderCode"] = folderCode;
							contentList.push(json);
						}
					}
					this.setData({
						"recommendList": contentList
					});
        } else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},


	bindListnerPlay: function(e) {
		//视频播放记录上传(整数分钟上传)
		var timePoints = e.detail.currentTime;
		//上一次播放时间
		var lastPlayTime = this.data.timeNow;
		//现在时间
		var time = new Date().getTime();
		var different = time - lastPlayTime;
		timePoints = Math.ceil(timePoints);
		this.setData({
			"currentTimePoints": timePoints
		});
		var lastTimePoints = this.data.lastTimePoints;
		if (timePoints > 0 && different > 60 * 1000) {
			//需要上传播放记录,同时将当前的时间点设置到timePoints
			if (timePoints != lastTimePoints) {
				this.setData({
					"lastTimePoints": timePoints,
					"timeNow": time
				});
				//只要上传就传60s,误差忽略
				this.uploadPlayRecord(timePoints);
			}
		}
	},


	onPlayOver: function(e) {
		//视频播放结束上传
		this.uploadPlayRecord(this.data.content.duration);
	},

	onUnload: function(e) {
		//页面被关闭时调用一次
		var currentTimePoints = this.data.currentTimePoints;
		if (currentTimePoints > 0) {
			//此处进度条时间不一定是整数分钟,按照实际的显示,播放时长还是按照60s计算
			this.uploadPlayRecord(currentTimePoints);
		}
	},


	uploadPlayRecord: function(timePoints) {
		var now = this.formatDate("yyyy-MM-wx hh:mm:ss", new Date());
		var params = {};
		if ("Topic" == this.data.fromPage) {
			params["code"] = this.data.topicId + "";
			params["subCode"] = this.data.code + "";
			params["subName"] = this.data.name + "";
			params["taskId"] = this.data.taskId + "";
			params["duration"] = 60;
			params["playTaskId"] = this.data.playTaskId + "";
			params["playtime"] = now + "";
			params["contentType"] = 1;
			params["durationTotal"] = this.data.content.duration + "";
			params["from"] = 1;
			params["type"] = "Topic";
			params["episodeNum"] = "";
			params["timePoints"] = Math.ceil(timePoints / 1000) + "";
			params["name"] = this.data.topicName + "";
			params = JSON.stringify(params);

			// params = '{"timestamp":"' + timestamp + '","nonce":"' + timestamp +
			// 	'","appKey":"1","corpId":"' + getApp().globalData.corpId +
			// 	'","userId":"' + getApp().globalData.userId + '","code":"'
			// 	+ this.data.topicId + '","subCode":"' + this.data.code +
			// 	'","subName":"' + this.data.name + '","taskId":"' +
			// 	this.data.taskId + '","duration":"' + 60 +
			// 	'","playTaskId":"' + this.data.playTaskId +
			// 	'","playtime":"' + now + '","contentType":"1","durationTotal":"' +
			// 	this.data.content.duration + '","from":"1","type":"' +
			// 	this.data.fromPage + '","episodeNum":"","timePoints":"' +
			// 	timePoints + '","name":"' + this.data.content.name + '",'

			// + '"sign":"appKey=1&code=' + this.data.parentId + '&nonce=' + timestamp + '&siteCode=' + getApp().globalData.siteCode + '&timestamp=' + timestamp + '&appSecret=' + getApp().globalData.appSecret + '"}';
		} else {
			params["code"] = this.data.code + "";
			// params["subCode"] = this.data.code + "";
			// params["subName"] = this.data.name + "";
			params["taskId"] = this.data.taskId + "";
			params["duration"] = 60;
			params["playTaskId"] = this.data.playTaskId + "";
			params["playtime"] = now + "";
			params["contentType"] = 1;
			params["durationTotal"] = this.data.content.duration + "";
			params["from"] = 1;
			params["type"] = "News";
			params["episodeNum"] = "";
			params["timePoints"] = Math.ceil(timePoints / 1000) + "";
			params["name"] = this.data.content.name + "";
			params = JSON.stringify(params);

			// params = '{"timestamp":"' + timestamp + '","nonce":"' + timestamp + '","appKey":"1","corpId":"' + getApp().globalData.corpId + '","userId":"' + getApp().globalData.userId + '","code":"' + this.data.code + '","subCode":"' + this.data.code + '","taskId":"' + this.data.taskId + '","duration":"' + 60 + '","playTaskId":"' + this.data.playTaskId + '","playtime":"' + now + '","contentType":"1","durationTotal":"' + this.data.content.duration + '","from":"1","type":"' + this.data.fromPage + '","episodeNum":"","timePoints":"' + timePoints + '","name":"' + this.data.content.name + '",'
			// 	+ '"sign":"appKey=1&code=' + this.data.parentId + '&nonce=' + timestamp + '&siteCode=' + getApp().globalData.siteCode + '&timestamp=' + timestamp + '&appSecret=' + getApp().globalData.appSecret + '"}';
		}
		var newUrl = getApp().globalData.dangjianUrl + "/member/userPlayDuration";

		this.httpGet(newUrl, params, "上传播放记录异常").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					console.log("上传播放记录成功");
        } else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},


	articleDetail(e) {
		var folderCode = e.currentTarget.dataset.foldercode;
		var type = e.currentTarget.dataset.type;
		var code = e.currentTarget.dataset.code;
		var name = e.currentTarget.dataset.name;
		var detailUrl = this.getUrl(type, code, folderCode, name);
		if (detailUrl.length > 0) {
			wx.navigateTo({
				url: detailUrl
			});
		} else { 
      this.showPageMsg("tips", "该内容类型详情页暂未开发") 
		}
	},


	// 页面被拉到底部
	onReachBottom: function(e) {
		this.showPageMsg("tips", "没有更多数据了！");
	},
});
